package com.lianboke.link.ad.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ProgressBar;

import com.lianboke.link.ad.R;
import com.lianboke.link.ad.Views.MyListView;
import com.lianboke.link.ad.VolleyUtils.HttpCallBack;
import com.lianboke.link.ad.VolleyUtils.HttpUtil;
import com.lianboke.link.ad.adapter.OrderTypeAdapter;
import com.lianboke.link.ad.app.MyApplication;
import com.lianboke.link.ad.beans.OrderListNewModel;
import com.lianboke.link.ad.common.CommonParams;
import com.lianboke.link.ad.utils.DeviceUtils;
import com.lianboke.link.ad.utils.KLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 类型任务 列表数据
 * Created by admin on 2018/1/5.
 */

public class OrderTypeListActivity   extends BaseActivity {

    @BindView(R.id.lv_order_1)
    public MyListView lv_order_1;

    @BindView(R.id.rv_swipe_refresh)
    public SwipeRefreshLayout rv_swipe_refresh;

    @BindView(R.id.loading_progress)
    public ProgressBar loading_progress;

    @BindView(R.id.view_empty)
    public View view_empty;


    private int iOrderType = 1;//任务类型
    private List<OrderListNewModel> orderTypeDatas = new ArrayList<OrderListNewModel>();
    private OrderTypeAdapter orderTypeAdapter;//类型任务数据适配器


    @Override
    public int getContentViewId() {
        return R.layout.activity_order_type_list;
    }

    @Override
    public void initActivityParams(Bundle savedInstanceState) {
        setTitleBackShow(true);
        iOrderType = getIntent().getIntExtra("data",1);
        String strContnt = getOrderTypeTitleContent(iOrderType);
        setTitleContent(strContnt);

        //设置数据适配器
        orderTypeAdapter = new OrderTypeAdapter(actContext);
        lv_order_1.setAdapter(orderTypeAdapter);


        //初始化为非刷新状态
        rv_swipe_refresh.setRefreshing(false);
        rv_swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
            @Override
            public void onRefresh() {
                //任务列表刷新
                rv_swipe_refresh.setRefreshing(true);
                getOrderTypeListDatasByNetwork();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取任务列表数据
        showLoadingProgress();
        getOrderTypeListDatasByNetwork();
    }

    /**
     * 获取类型任务title
     * @param itype
     * @return
     */
    private String getOrderTypeTitleContent(int itype){
        String strorderTypeTitle = "";
        switch (iOrderType){
            case 1:
                strorderTypeTitle = "装机任务列表";
                break;
            case 2:
                strorderTypeTitle = "注册任务列表";
                break;
            case 3:
                strorderTypeTitle = "试玩任务列表";
                break;
            case 4:
                strorderTypeTitle = "互助任务列表";
                break;
        }
        return strorderTypeTitle;
    }
        /**
         * 显示加载框
         */
    private void showLoadingProgress(){
        if(loading_progress!=null && loading_progress.getVisibility()!= View.VISIBLE){
            loading_progress.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏家在狂
     */
    private void hideLoadingProgress(){
        if(loading_progress!=null && loading_progress.getVisibility()!=View.GONE){
            loading_progress.setVisibility(View.GONE);
        }
    }
    /**
     * 获取任务别表
     */
    public void getOrderTypeListDatasByNetwork(){
        HashMap<String,String> params = new HashMap<String,String>();
        params.put("deviceid", DeviceUtils.getImei(appContext));
        params.put("version",DeviceUtils.getVersionName(actContext));
        params.put("nettypename",DeviceUtils.getDeviceAc());
        params.put("type",String.valueOf(iOrderType));
        params.put("city", MyApplication.strCity);
        HttpUtil.getInstance().request_get(CommonParams.URL_WEB_TYPE_ORDER_LIST, params, new HttpCallBack<List<OrderListNewModel>>() {
            @Override
            public void onSuccess(List<OrderListNewModel> data) {
                hideLoadingProgress();
                rv_swipe_refresh.setRefreshing(false);
                if(data!=null && !data.isEmpty()){
                    hideEmptyDatasView();
                    if(orderTypeDatas!=null){
                        orderTypeDatas.clear();
                    }
                    orderTypeDatas.addAll(data);
                    //设置数据
                    orderTypeAdapter.setOrderDatas(orderTypeDatas);
                }else{
                    showEmptyDatasView();
                }
            }

            @Override
            public void onFail(String msg) {
                rv_swipe_refresh.setRefreshing(false);
                hideLoadingProgress();
                showEmptyDatasView();
                showFinishDialog(msg);
            }
        });
    }

    /**
     * 显示空数据view
     */
    public void showEmptyDatasView(){
        if(rv_swipe_refresh!=null){
            rv_swipe_refresh.setVisibility(View.GONE);
        }
        if(view_empty!=null){
            view_empty.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏空数据view
     */
    public void hideEmptyDatasView(){
        if(rv_swipe_refresh!=null){
            rv_swipe_refresh.setVisibility(View.VISIBLE);
        }
        if(view_empty!=null){
            view_empty.setVisibility(View.GONE);
        }
    }
    @Override
    public void getTimerOrderdMyBroadcastReceiver(String action) {
        KLog.e("----------getTimerOrderdMyBroadcastReceiver-----------:" + action);
        if ("android.myapp.timer.orders.auto.start".equals(action)) {
            //结束本页面
            finish();
        }
    }

}

